package client;
import io.netty.bootstrap.Bootstrap;

import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;

import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.*;

public class NettyClient {

    public static String contact(){
    EventLoopGroup group = new NioEventLoopGroup(1);
    Bootstrap bootstrap = new Bootstrap();
    final HttpClientHandler httpClientHandler = new HttpClientHandler();
    try {
        bootstrap.group(group)
                .channel(NioSocketChannel.class)
                .option(ChannelOption.SO_KEEPALIVE, true)
                .handler(new ChannelInitializer<Channel>() {

                    @Override
                    protected void initChannel(Channel channel)
                            throws Exception {
                        channel.pipeline().addLast(new HttpClientCodec());
                        channel.pipeline().addLast(new HttpObjectAggregator(65536));
                        channel.pipeline().addLast(httpClientHandler);
                    }
                });
        ChannelFuture future = bootstrap.connect("127.0.0.1", 8086);
        future.channel().closeFuture().sync();

    } catch (Exception e) {
        e.printStackTrace();
    }finally{
        group.shutdownGracefully();
        return httpClientHandler.getResult();
    }
    }
}
